package com.example.sunmiv1.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.OnlineOrderPopupAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.OrderDetailInfo;
import com.example.sunmiv1.entity.ReportLossGoodBean;
import com.example.sunmiv1.entity.TurnoverBean;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OnlineOrderDetailActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.orderDetail_listView)
    ListView orderDetailListView;

    private Context context;
    private ACache aCache;
    private LoadingDialog loadingDialog;
    private OnlineOrderPopupAdapter onlineOrderPopupAdapter;
//    private OnlineOrderBean.OrderBean orderBean;
    private TurnoverBean.OrderBean orderBean;
    private List<TurnoverBean.OrderBean.GoodsInfoBean> list = new ArrayList<>();

    @Override
    protected void initView() {
        setContentView(R.layout.activity_online_order_detail);
        ButterKnife.bind(this);
        titleTitle.setText("訂單詳情");
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
    }

    @Override
    protected void initData() {
        orderBean = (TurnoverBean.OrderBean) getIntent().getSerializableExtra("good");
        list.addAll(orderBean.getGoods_info());
        setAdapter();
    }

    @Override
    protected void initListener() {

    }

    private void setAdapter() {
        onlineOrderPopupAdapter = new OnlineOrderPopupAdapter(context, list);
        orderDetailListView.setAdapter(onlineOrderPopupAdapter);
        onlineOrderPopupAdapter.setChoose(index -> {
            Intent intent = new Intent(context, StringCodeOperationActivity.class);
            intent.putExtra("operation", 28);
            intent.putExtra("good", list.get(index));
            context.startActivity(intent);
        });
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    TurnoverBean.OrderBean.GoodsInfoBean bean = (TurnoverBean.OrderBean.GoodsInfoBean) data.getSerializableExtra("bean");
                    int index = 0;
                    for (int t = 0; t < list.size(); t++) {
                        if (bean.getGoods_id().equals(list.get(t).getGoods_id())) {
                            if (!TextUtils.isEmpty(bean.getSku_id())) {
                                if (bean.getSku_id().equals(list.get(t).getSku_id())) {
                                    index = t;
                                    break;
                                }
                            } else {
                                index = t;
                                break;
                            }
                        }
                    }
                    list.set(index, bean);
                    setAdapter();
                    break;
            }
        }

    }

    /*private void getDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id", getIntent().getStringExtra("id"));
            HttpUtils.post(context, Common.SELL_LIST, Tools.createParameterCode(jsonObject), new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    orderDetailInfo = GsonUtils.JsonToBean(text, OrderDetailInfo.class);
                    if (orderDetailInfo.getStatus() == 1) {
                        list.addAll(orderDetailInfo.getData());
                        setAdapter();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }*/

}
